package com.dml.webmagicd.webmagic.Processor;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.model.HttpRequestBody;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Json;
import us.codecraft.webmagic.selector.Selectable;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class JuejinProcessor implements PageProcessor {

  private final Site site = Site.me().setRetryTimes(3).setSleepTime(1000 * 60);

  private String listReg = "https://api\\.juejin\\.cn/booklet_api/v1/booklet/get\\?aid=\\d+&uuid=\\d+&spider=0";

  @Override
  public void process(Page page) {
    Selectable url = page.getUrl();
    if (url.regex(listReg).match()) {
      Json json = page.getJson();
      List<Selectable> nodes = json.jsonPath("data.sections").nodes();
      nodes.forEach(node -> {
        Json json1 = new Json(node.get());
        String booklet_id = json1.jsonPath("booklet_id").get();
        String section_id = json1.jsonPath("section_id").get();
        String title = json1.jsonPath("title").get();
        Request request = new Request(
        "https://api.juejin.cn/booklet_api/v1/section/get?aid=2608&uuid=7318006475904419338&spider=0"
        );
        request.setMethod("POST");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("section_id", section_id);
        String jsonString = jsonObject.toJSONString();
        request.setRequestBody(HttpRequestBody.json(jsonString, "UTF-8"));
        int i = nodes.indexOf(node);
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
//        stringObjectHashMap.put("booklet_id", booklet_id);
        stringObjectHashMap.put("title", title);
        stringObjectHashMap.put("section_index", i);
        request.setExtras(stringObjectHashMap);
        page.addTargetRequest(request);
        page.setSkip(true);
      });
    } else {
      Map<String, Object> extras = page.getRequest().getExtras();
      String title = (String) extras.get("title");
      int sectionIndex = (int) extras.get("section_index");
      Json json = page.getJson();
      String content = json.jsonPath("data.section.markdown_show").toString();
      page.putField("title", title);
      page.putField("sectionIndex", sectionIndex);
      page.putField("content", content);
    }
  }

  @Override
  public Site getSite() {
    site.setCharset("UTF-8");
    site.setUserAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36");
    site.addHeader("Cookie", "__tea_cookie_tokens_2608=%257B%2522web_id%2522%253A%25227318006475904419338%2522%252C%2522user_unique_id%2522%253A%25227318006475904419338%2522%252C%2522timestamp%2522%253A1703856170533%257D; sid_guard=f9d91140b6ee9284e0d8227500a1c107%7C1705204944%7C31536000%7CMon%2C+13-Jan-2025+04%3A02%3A24+GMT; uid_tt=b0b8b37d949c57baa7e646379d24217c; uid_tt_ss=b0b8b37d949c57baa7e646379d24217c; sid_tt=f9d91140b6ee9284e0d8227500a1c107; sessionid=f9d91140b6ee9284e0d8227500a1c107; sessionid_ss=f9d91140b6ee9284e0d8227500a1c107; sid_ucp_v1=1.0.0-KGQxZjY2ZTg0Y2ZhYmU1ZjU5OGY5N2I5ZTNkOWY5MmU3OThjZDA4OTUKFQiHpKCnjPRzENC5ja0GGLAUOAhACxoCbGYiIGY5ZDkxMTQwYjZlZTkyODRlMGQ4MjI3NTAwYTFjMTA3; ssid_ucp_v1=1.0.0-KGQxZjY2ZTg0Y2ZhYmU1ZjU5OGY5N2I5ZTNkOWY5MmU3OThjZDA4OTUKFQiHpKCnjPRzENC5ja0GGLAUOAhACxoCbGYiIGY5ZDkxMTQwYjZlZTkyODRlMGQ4MjI3NTAwYTFjMTA3; store-region=cn-sx; store-region-src=uid; _ga=GA1.2.570845530.1705204945; _ga_S695FMNGPJ=GS1.2.1705204945.1.0.1705204945.60.0.0; _tea_utm_cache_2018={%22utm_source%22:%22course_center%22}; _tea_utm_cache_2608={%22utm_source%22:%22course_center%22}; csrf_session_id=5db96eca48fd975c261231b925022aec; msToken=LNek8sF9GiBXNeVQsumASL0prEsa5WTqONMA3cKHBXlXkA0Sk1jTF8Mjza1bycxGDiTL-qzUp2alN3Ng5LBnJaymrKEYPkSbwnXyNFuYhVbXVWVD-T5pqkVMHH5vpk4=");
    return site;
  }
}
